package cn.atguigu.partitioner;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * @author JiDingXia
 * @date 2025-02-23 03:40:42
 */
public class CustomPartitioner implements Partitioner {
	/**
	 *
	 * @param topic
	 * @param key
	 * @param keyBytes 序列化后的key
	 * @param value
	 * @param valueBytes 序列化后的value
	 * @param cluster
	 * @return
	 */
	@Override
	public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
		String keyString = key.toString();
		int hashCode = keyString.hashCode();
		Integer partitionCount = cluster.partitionCountForTopic(topic);
		//返回分区号,从0开始直到partitionCount-1
		return hashCode % partitionCount;
	}

	@Override
	public void close() {

	}

	@Override
	public void configure(Map<String, ?> map) {

	}
}
